/*
 * Service.java
 *
 * Created on 29 March 2012, 08:45
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Simulation.Services.Main;

import Simulation.Messages.HealthMessage;
import Simulation.Messages.MessageCommonConstants;
import Simulation.Messages.Message;
import Simulation.Messages.HealthMessage;
import Simulation.Messages.PerformanceMessage;
import Simulation.Services.Common.ServiceComms;

/**
 *
 * @author aking2
 */
public class Service extends Thread {
    
   
@Override
public void run() {    
}    
    

public byte GetSimularorId() {
    return storedSimulatorId;
}
    
public byte GetServiceId() {
    return storedServiceId;
}

public byte GetSimulationState() {
    return storedSimulationState;
}

public byte GetHealthStatus() {
    return storedHealthState;
}

public void SetServiceId(byte serviceId) {
    storedServiceId = serviceId;
}

public void SetSimularorId(byte simId) {
    storedSimulatorId = simId;
}

public void SetSimulationState(byte simState) {
    storedSimulationState = simState;
}

public void SetHealthStatus(byte health) {
    storedHealthState = health;
}



public void ProcessCommonMessage(Simulation.Messages.Message message) {
    switch( message.messageID ) {
        case MESSAGE_TIMEOUT:
            HandleTimeout();
            break;

        case IOS_START_SIMULATING:
            HandleStartSimulating();
            break;                                        

        case IOS_STOP_SIMULATING:
            HandleStopSimulating();
            break;

        default:
            break;
    }       
}
 
public void SetMonitorPerformance(boolean setting) {
    monitiorPerformance = setting;
}

public void ProcessInputsPerformanceStartOf() {
            // Record the start time
        if(monitiorPerformance)
            processInputsStartTime = System.nanoTime();
}

public void ProcessInputsPerformanceEndOf() {
        // Record the finish time and report the performance results
        if(monitiorPerformance) {
            processInputsFinishTime = System.nanoTime();
            
            // Enforce some difference, so that we don't
            // get a Zero under normal operation
            if( processInputsStartTime == processInputsFinishTime)
                processInputsFinishTime += 1;
            
            PerformanceMessage pm = new PerformanceMessage(
              GetSimularorId(),
              GetServiceId(),
              PerformanceMessage.PERFORMANCE_PROCESS_INPUTS,
              processInputsStartTime,
              processInputsFinishTime);
            comms.SendMessage(pm);
        }      
}

public void SetIosHealthSendPeriod( int  setting) {
    iosHealthSendPeriod = setting;
}
public void SendOutputsPerformanceStartOf() {
        // Record the start time
        if(monitiorPerformance)
            sendOutputsStartTime = System.nanoTime();    
}

public void SendOutputsPerformanceEndOf() {
        // Record the finish time and report the performance results
        if(monitiorPerformance) {
            sendOutputsFinishTime = System.nanoTime();
            
            // Enforce some difference, so that we don't
            // get a Zero under normal operation
            if( sendOutputsStartTime == sendOutputsFinishTime)
                sendOutputsFinishTime += 1;            
            
            PerformanceMessage pm = new PerformanceMessage(
              GetSimularorId(),
              GetServiceId(),
              PerformanceMessage.PERFORMANCE_SEND_OUTPUTS,
              sendOutputsStartTime,
              sendOutputsFinishTime);
            comms.SendMessage(pm);
        }      
}

public void SendStatusMessage() {
        long timeNow = System.nanoTime();
        if( timeNow >= (iosHealthSendPeriod + lastTimeSentStatusMessage)) {
            
            // Time to send the message
            comms.SendMessage(
                new HealthMessage(
                    GetSimularorId(),
                    GetServiceId(),
                    GetSimulationState(),
                    GetHealthStatus()
                )
            );  
            lastTimeSentStatusMessage = timeNow;
            
        }
    }

private void HandleTimeout() {

}

private void HandleStartSimulating() {
    if( storedSimulationState == MessageCommonConstants.SIM_STATE_CONFUGURED) {
        storedSimulationState = MessageCommonConstants.SIM_STATE_SIMULATING;
    }
}

private void HandleStopSimulating() {
    if( storedSimulationState == MessageCommonConstants.SIM_STATE_SIMULATING) {
        storedSimulationState = MessageCommonConstants.SIM_STATE_CONFUGURED;
    }
}


private byte storedServiceId            = MessageCommonConstants.UNKNOWN_SERVICE;
private byte storedSimulatorId          = 0;
private byte storedHealthState          = HealthMessage.STATUS_UNKNOWN;
private byte storedSimulationState      = MessageCommonConstants.SIM_STATE_UNDEFINED;
private boolean monitiorPerformance     = false;
private long processInputsStartTime     = 0;
private long processInputsFinishTime    = 0;
private long sendOutputsStartTime       = 0;
private long sendOutputsFinishTime      = 0;
private long lastTimeSentStatusMessage  = 0;
private int  iosHealthSendPeriod        = 10000;  

public ServiceComms comms;   

}
